package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;


import android.accessibilityservice.GestureDescription;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.Path;
import android.graphics.Rect;
import android.os.Bundle;

import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.view.accessibility.AccessibilityNodeInfo;
import android.widget.Button;

import com.example.accessibility.DarknessMagic;
import com.example.accessibility.MyAccess;
import com.example.accessibility.UniAccessibility;

import java.util.List;

import io.dcloud.feature.uniapp.UniSDKInstance;
import io.dcloud.feature.uniapp.bridge.UniJSCallback;


public class MainActivity extends AppCompatActivity {
    Context context=this;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button=findViewById(R.id.button);

        UniAccessibility accessibility=new UniAccessibility();
        accessibility.mUniSDKInstance=new UniSDKInstance(this);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //read
                //accessibility.ReadView(null);
                //accessibility.read();



                accessibility.ReadView(new UniJSCallback() {
                    @Override
                    public void invoke(Object data) {
                        String text = data.toString();

                        for (int i=1000;i<text.length();) {

                            Log.e("TAG", "invoke: "+text.substring(0,i));
                            text=text.substring(i);
                            i=i+800;
                        }

                        Log.e("TAG", "结束: "+text);
                    }

                    @Override
                    public void invokeAndKeepAlive(Object data) {

                    }
                });


                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Log.e("TAG", "run: " );
                        while (true) {
                            try {
                                Thread.sleep(1000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }).start();
            }
        });
        findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //点击
//                accessibility.ClickView("com.example.myapplication:id/button",null);
                //accessibility.toapp("com.taobao.idlefish",MainActivity.this);

//                accessibility.xianyu("12345", 5, new UniJSCallback() {
//                    @Override
//                    public void invoke(Object data) {
//
//                    }
//
//                    @Override
//                    public void invokeAndKeepAlive(Object data) {
//
//                    }
//                });

//
//                Intent intent=new Intent(Intent.ACTION_MAIN);
//                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                intent.addCategory(Intent.CATEGORY_LAUNCHER);
//                //参数是包名，类全限定名，注意直接用类名不行
//                ComponentName cn=ComponentName.unflattenFromString("com.tencent.mobileqq/.activity.SplashActivity");
//                //ComponentName cn=ComponentName.unflattenFromString("com.taobao.idlefish/com.idlefish.flutterbridge.flutterboost.boost.FishFlutterBoostActivity");
//                intent.setComponent(cn);

                Intent intent = accessibility.mUniSDKInstance.getContext().getPackageManager().getLaunchIntentForPackage("com.tencent.mobileqq");
                accessibility.mUniSDKInstance.getContext().startActivity(intent);


                accessibility.myAccess.set_Click(new String[]{"动态"});
                accessibility.myAccess.set_Click(new String[]{"好友动态"});
                accessibility.myAccess.set_Click(new String[]{"加号按钮"});

                accessibility.myAccess.setClick_seriescallback(new MyAccess.click_seriescallback() {
                    @Override
                    public void onclickseriescall() {
                        accessibility.myAccess.fine_view(new String[]{"com.tencent.mobileqq:id/hkk"}, new MyAccess.FindCallback() {
                            @Override
                            public void onfindcall(List<AccessibilityNodeInfo> list) {
                                if (list.size()!=0)
                                {
                                    for (int i = 0; i < list.size(); i++) {
                                        DarknessMagic.AccessibilityServiceUtils.clickView(list.get(0));
                                    }

                                    accessibility.myAccess.set_Click(new String[]{"照片/视频","com.tencent.mobileqq:id/ht9"});
                                    accessibility.myAccess.setClick_seriescallback(new MyAccess.click_seriescallback() {
                                        @Override
                                        public void onclickseriescall() {

                                        }
                                    });
                                }

                            }
                        });


                    }
                });


//                accessibility.kuaishou("4561212", 6, new UniJSCallback() {
//                    @Override
//                    public void invoke(Object data) {
//
//                    }
//
//                    @Override
//                    public void invokeAndKeepAlive(Object data) {
//
//                    }
//                });


            }
        });

        if (!MyAccess.isStart())
        {
            try {

                this.startActivity(new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS));
            } catch (Exception e) {
                this.startActivity(new Intent(Settings.ACTION_SETTINGS));
                e.printStackTrace();
            }
            return;
        }

    }

}











//                Intent intent=new Intent(Intent.ACTION_MAIN);
//                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                intent.addCategory(Intent.CATEGORY_LAUNCHER);
//                //参数是包名，类全限定名，注意直接用类名不行
//                ComponentName cn=ComponentName.unflattenFromString("com.taobao.idlefish/.maincontainer.activity.MainActivity");
//                //ComponentName cn=ComponentName.unflattenFromString("com.taobao.idlefish/com.idlefish.flutterbridge.flutterboost.boost.FishFlutterBoostActivity");
//                intent.setComponent(cn);
//                startActivity(intent);
//
//                String[] strings=new String[2];
//                strings[0]="卖闲置";
//                strings[1]="com.taobao.idlefish:id/post_click";
//                accessibility.myAccess.click_view(strings, new MyAccess.ClickCallback() {
//                    @Override
//                    public void onclick_finshcall(boolean b) {
//                        if (b){
//                            strings[0]="发闲置";
//                            strings[1]="30s发布宝贝";
//                            accessibility.myAccess.click_view(strings, new MyAccess.ClickCallback() {
//                                @Override
//                                public void onclick_finshcall(boolean b) {
//                                    if (b){
//                                        strings[0]="";
//                                        strings[1]="30s发布宝贝";
//                                        accessibility.myAccess.click_view(strings, new MyAccess.ClickCallback() {
//                                            @Override
//                                            public void onclick_finshcall(boolean b) {
//                                                if (b){
//                                                    strings[0]="添加图片";
//                                                    strings[1]="android.widget.ImageView";
//                                                    accessibility.myAccess.click_view(strings, new MyAccess.ClickCallback() {
//                                                        @Override
//                                                        public void onclick_finshcall(boolean b) {
//                                                            if (b){
//                                                                strings[0]="选择";
//                                                                strings[1]="android.view.View";
//                                                                accessibility.myAccess.fine_view(strings, new MyAccess.FindCallback() {
//                                                                    @Override
//                                                                    public void onfindcall(List<AccessibilityNodeInfo> list) {
//                                                                        for (int i = 0; i < list.size(); i++) {
//                                                                            DarknessMagic.AccessibilityServiceUtils.clickView(list.get(i));
//                                                                        }
//                                                                        strings[0]="下一步";
//                                                                        strings[1]="com.taobao.idlefish";
//
//                                                                        accessibility.myAccess.click_view(strings, new MyAccess.ClickCallback() {
//                                                                            @Override
//                                                                            public void onclick_finshcall(boolean b) {
//                                                                                if (b) {
//                                                                                    strings[0]="完成";
//                                                                                    strings[1]="android.widget.Button";
//                                                                                    try {
//                                                                                        Thread.sleep(2000);
//                                                                                    } catch (InterruptedException e) {
//                                                                                        e.printStackTrace();
//                                                                                    }
//                                                                                    accessibility.myAccess.click_view(strings, new MyAccess.ClickCallback() {
//                                                                                        @Override
//                                                                                        public void onclick_finshcall(boolean b) {
//                                                                                            if (b){
//                                                                                                try {
//                                                                                                    Thread.sleep(2000);
//                                                                                                } catch (InterruptedException e) {
//                                                                                                    e.printStackTrace();
//                                                                                                }
//                                                                                                strings[0]="描述一下";
//                                                                                                strings[1]="com.taobao.idlefish";
//                                                                                                accessibility.myAccess.fine_view(strings, new MyAccess.FindCallback() {
//                                                                                                    @Override
//                                                                                                    public void onfindcall(List<AccessibilityNodeInfo> list) {
//                                                                                                        for (int i = 0; i < list.size(); i++) {
//                                                                                                            Log.e("123", "onfindcall: "+list.get(i) );
//                                                                                                            ClipboardManager clipboard = (ClipboardManager)context.getSystemService(Context.CLIPBOARD_SERVICE);
//                                                                                                            ClipData clip = ClipData.newPlainText("text", "789");
//                                                                                                            clipboard.setPrimaryClip(clip);
//
//                                                                                                        }
//                                                                                                    }
//                                                                                                });
//
//
//
//                                                                                            }
//                                                                                        }
//                                                                                    });
//                                                                                }
//                                                                            }
//                                                                        });
//                                                                    }
//                                                                });
//                                                            }
//                                                        }
//                                                    });
//                                                }
//                                            }
//                                        });
//                                    }
//                                }
//                            });
//                        }
//                    }
//                });
////                DarknessMagic.AccessibilityServiceUtils.clickView(child);
////                DarknessMagic.AccessibilityServiceUtils accessibilityServiceUtils[] =new DarknessMagic.AccessibilityServiceUtils[1];
////                accessibilityServiceUtils[0]=DarknessMagic.AccessibilityServiceUtils.newId("com.taobao.idlefish:id/post_click");
////                //DarknessMagic.AccessibilityServiceUtils.newId("com.taobao.idlefish:id/post_click");;
////
////                DarknessMagic.AccessibilityServiceUtils.clickView(accessibilityServiceUtils[0].findFirst(accessibilityServiceUtils,accessibility.myAccess.Info));
//                //accessibility.xianyu();

























//
//
//
//    Intent intent = getPackageManager().getLaunchIntentForPackage("com.wuba.zhuanzhuan");
//                accessibility.mUniSDKInstance.getContext().startActivity(intent);
//
//
//
//
//
//                accessibility.myAccess.set_Click(new String[]{
//        "ackageName: com.wuba.zhuanzhuan; className: android.view.View; text: null; error: null; maxTextLength: -1; contentDescription: null; tooltipText: null; viewIdResName: com.wuba.zhuanzhuan:id/ew5; checkable: false; checked: false; focusable: true; focused: false; selected: false; clickable: true; longClickable: false; contextClickable: false; enabled: true; password: false; scrollable: false; importantForAccessibility: true; visible: true; actions: [AccessibilityAction: ACTION_FOCUS - null, AccessibilityAction: ACTION_SELECT - null, AccessibilityAction: ACTION_CLEAR_SELECTION - null, AccessibilityAction: ACTION_CLICK - null, AccessibilityAction: ACTION_ACCESSIBILITY_FOCUS - null, AccessibilityAction: ACTION_SHOW_ON_SCREEN - null]"
//    });
//
//                accessibility.myAccess.set_Click(new String[]{
//        "发布闲置宝贝"
//    });
//                accessibility.myAccess.set_Click(new String[]{
//        "拍摄宝贝靓照",
//                "android.widget.TextView",
//                "com.wuba.zhuanzhuan:id/e91"
//
//    });
//                accessibility.myAccess.set_Click(new String[]{
//        "相册",
//                "viewIdResName: com.wuba.zhuanzhuan:id/dgt"
//    });
//                accessibility.myAccess.setClick_seriescallback(new MyAccess.click_seriescallback() {
//        @Override
//        public void onclickseriescall() {
//            try {
//                Thread.sleep(1000);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//            accessibility.myAccess.fine_view(new String[]{
//                    "viewIdResName: com.wuba.zhuanzhuan:id/ce",
//                    "className: android.widget.TextView;",
//                    "text: null;"
//            }, new MyAccess.FindCallback() {
//                @Override
//                public void onfindcall(List<AccessibilityNodeInfo> list) {
//                    for (int i = 0; i < list.size(); i++) {
//                        DarknessMagic.AccessibilityServiceUtils.clickView(list.get(i));
//                    }
//
//                    accessibility.myAccess.set_Click(new String[]{
//                            "下一步",
//                            "com.wuba.zhuanzhuan:id/dti",
//                    });
//                    accessibility.myAccess.set_Click(new String[]{
//                            "com.wuba.zhuanzhuan:id/cg",
//
//                    });
//                    accessibility.myAccess.set_Click(new String[]{
//                            "确定",
//                            "com.wuba.zhuanzhuan:id/ej2",
//                    });
//
//                    accessibility.myAccess.setClick_seriescallback(new MyAccess.click_seriescallback() {
//                        @Override
//                        public void onclickseriescall() {
//
//                        }
//                    });
//                }
//            });
//        }
//    });
//}